package com.atguigu.gmall.activity.redis;
/**
 * 加油！你是最胖的
 *
 * @author TaiXiaoDong on 2023/3/24.
 * @version 1.0
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**



 */
@Configuration
public class RedisChannelConfig {

    /**
     注入主题订阅

    */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter listenerAdapter){

        RedisMessageListenerContainer container=new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(listenerAdapter,new PatternTopic("seckillpush"));

        return container;
    }
    /*@Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter listenerAdapter){

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        container.addMessageListener(listenerAdapter,new PatternTopic("seckillpush"));

        return container;
    }*/

    /**
     监听适配器

    */
    @Bean
    public MessageListenerAdapter listenerAdapter(MessageReceive messageReceive) {

        //使用反射的方法调用指定的方法

    return new MessageListenerAdapter(messageReceive,"receiveMessage");
    }


    /**
     注入连接工厂

    */
    @Bean
    public StringRedisTemplate template(RedisConnectionFactory factory){

        return new StringRedisTemplate(factory);

    }

}
